package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.Assets;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.THouseAssets;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TLeaseManage;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 租赁管理基本信息Controller
 * 
 * @author ruoyi
 * @date 2024-03-18
 */
@Controller
@RequestMapping("/system/manage")
public class TLeaseManageController extends BaseController
{
    private String prefix = "system/manage";

    @Autowired
    private ITLeaseManageService tLeaseManageService;
    @Autowired
    private ITHouseAssetsService itHouseAssetsService;
    @Autowired
    private IAssetsService assetsService;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private ISysDictDataService dictDataService;

    @RequiresPermissions("system:manage:view")
    @GetMapping()
    public String manage(ModelMap mmap)
    {
        mmap.put("key","house");
        return prefix + "/manage";
    }

    @RequestMapping("/getLeaseAcreage/{type}/{deptId}")
    @ResponseBody
    public AjaxResult getLeaseAcreage(@PathVariable("type") String type,@PathVariable("deptId") Long deptId)
    {
        Assets assets = assetsService.selectDeptById(deptId);
        if (assets.getRentalArea() != null) {
            assets.setResidualArea(assets.getTotal().subtract(assets.getRentalArea()));
        } else {
            assets.setResidualArea(assets.getTotal());
        }
        return success(assets);
    }

    @RequestMapping("/getHouseAssetsId/{type}/{deptId}")//TableDataInfo
    @ResponseBody
    public AjaxResult getHouseAssetsId(@PathVariable("type") String type,@PathVariable("deptId") Long deptId)
    {
        THouseAssets houseAssets = new THouseAssets();
        houseAssets.setType(Long.valueOf(type));
        List<THouseAssets> list = itHouseAssetsService.selectTHouseAssetsList(houseAssets);
        return success(list);
    }

    @RequestMapping("/getHouseAcrage/{type}/{deptId}")//TableDataInfo
    @ResponseBody
    public AjaxResult getHouseAcrage(@PathVariable("type") String type,@PathVariable("deptId") Long deptId)
    {
        TLeaseManage leaseManage = new TLeaseManage();
        Assets assets = assetsService.selectDeptById(Long.valueOf(type));
        if (assets.getRentalArea() != null) {
            //有以租赁面积  可租赁面积=总面积减去以租赁面积
            leaseManage.setTotal(assets.getTotal().subtract(assets.getRentalArea()));
        } else {
            leaseManage.setTotal(assets.getTotal());
        }
        leaseManage.setAcreage(leaseManage.getTotal());
        return success(leaseManage);
    }

    /**
     * 查询租赁管理基本信息列表
     */
    @RequiresPermissions("system:manage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TLeaseManage tLeaseManage,Long deptId)
    {
        startPage();
        if (deptId != 205) {//房产资产 租赁只有房产可以租赁  地产没有租赁 此处为填写默认值205  不具有任何意义
            tLeaseManage.setTypeId(deptId);
        }
        List<TLeaseManage> list = tLeaseManageService.selectTLeaseManageListByTypeId(tLeaseManage);
        return getDataTable(list);
    }

    /**
     * 导出租赁管理基本信息列表
     */
    @RequiresPermissions("system:manage:export")
    @Log(title = "租赁管理基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TLeaseManage tLeaseManage)
    {
        List<TLeaseManage> list = tLeaseManageService.selectTLeaseManageList(tLeaseManage);
        ExcelUtil<TLeaseManage> util = new ExcelUtil<TLeaseManage>(TLeaseManage.class);
        return util.exportExcel(list, "租赁管理基本信息数据");
    }

    /**
     * 新增租赁管理基本信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/addbyDeptId/{deptId}")
    public String addbyDeptId(@PathVariable("deptId")long deptId,ModelMap mmap) {
        mmap.put("deptId", deptId);
        THouseAssets house = new THouseAssets();
        house.setType(deptId);
        List<THouseAssets> houseList = itHouseAssetsService.selectTHouseAssetsList(house);
        mmap.put("houseList",houseList);
        mmap.put("THouseAssets",house);
        return prefix + "/add";
    }

    /**
     * 新增保存租赁管理基本信息
     */
    @RequiresPermissions("system:manage:add")
    @Log(title = "租赁管理基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TLeaseManage tLeaseManage)
    {
        if (tLeaseManage.getId() == null){
            tLeaseManage.setId(generateRandomNumber());
        }
        return toAjax(tLeaseManageService.insertTLeaseManage(tLeaseManage));
    }


    @GetMapping("/editbyDeptId/{id}/{deptId}")
    public String editbyDeptId(@PathVariable("id") Long id,@PathVariable("deptId")long deptId,ModelMap mmap) {
        TLeaseManage tLeaseManage = tLeaseManageService.selectTLeaseManageById(id);
        THouseAssets tHouseAssets = itHouseAssetsService.selectTHouseAssetsById(Long.valueOf(tLeaseManage.getAssetsId()));
        Long type = tHouseAssets.getType();
        Assets assets = assetsService.selectDeptById(type);
        if (assets.getRentalArea() != null) {
            tLeaseManage.setTotal(assets.getTotal().subtract(assets.getRentalArea()).add(tLeaseManage.getAcreage()));
        }else {
            tLeaseManage.setTotal(assets.getTotal());
        }
        THouseAssets house = new THouseAssets();
        house.setType(type);
        List<THouseAssets> houseList = itHouseAssetsService.selectTHouseAssetsList(house);
        mmap.put("tLeaseManage", tLeaseManage);
        mmap.put("houseList",houseList);
        mmap.put("deptId", deptId);
        mmap.put("assets", assets);
        return prefix + "/edit";
    }

    /**
     * 修改租赁管理基本信息
     */
    @RequiresPermissions("system:manage:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TLeaseManage tLeaseManage = tLeaseManageService.selectTLeaseManageById(id);
        mmap.put("tLeaseManage", tLeaseManage);
        return prefix + "/edit";
    }

    /**
     * 修改保存租赁管理基本信息
     */
    @RequiresPermissions("system:manage:edit")
    @Log(title = "租赁管理基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TLeaseManage tLeaseManage)
    {
        return toAjax(tLeaseManageService.updateTLeaseManage(tLeaseManage));
    }

    /**
     * 删除租赁管理基本信息
     */
    @RequiresPermissions("system:manage:remove")
    @Log(title = "租赁管理基本信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tLeaseManageService.deleteTLeaseManageByIds(ids));
    }
}
